@echo off
rem 使用默认的ANSI编码

echo ===================================================
echo             Microphone SDK Tool
echo ===================================================
echo.

set PORT=3000

if "%~1"=="" (
    echo Starting Microphone SDK server...
    echo.
    goto :start
)

if /i "%~1"=="start" goto :start
if /i "%~1"=="clean" goto :clean
if /i "%~1"=="install" goto :install
if /i "%~1"=="check" goto :check
if /i "%~1"=="kill" goto :kill
if /i "%~1"=="help" goto :help

echo Unknown command: %~1
echo Use %~nx0 help to see available commands
goto :eof

:help
echo Microphone SDK Usage:
echo.
echo Available commands:
echo   start   - Start development server
echo   clean   - Clean cache and start server
echo   install - Install dependencies
echo   check   - Check port usage
echo   kill    - Release occupied port
echo   help    - Show help information
echo.
echo Note: Double-clicking the script will execute the start command by default
echo.
goto :eof

:start
echo Starting Microphone SDK development server...
echo.
echo After server starts, you can use it in UE5:
echo 1. Load web URL in Web UI: http://localhost:%PORT%
echo 2. Control via window.MicrophoneControl object
echo.
set BROWSER=none
npm start
goto :eof

:clean
echo Cleaning cache...
if exist node_modules\.cache rmdir /s /q node_modules\.cache
if exist build rmdir /s /q build
echo Cache cleaning completed.
echo.
goto :start

:install
echo Installing dependencies...
npm install
echo.
goto :eof

:check
echo Checking if port %PORT% is in use...
netstat -ano | findstr ":%PORT% .*LISTENING"
if %errorlevel% equ 0 (
    echo Port %PORT% is in use.
) else (
    echo Port %PORT% is not in use.
)
goto :eof

:kill
echo Checking and releasing port %PORT%...
for /f "tokens=5" %%a in ('netstat -ano ^| findstr ":%PORT% .*LISTENING"') do (
    echo Terminating process PID: %%a
    taskkill /F /PID %%a
)
echo Port check completed.
goto :eof 